﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RSA文件加密
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
            
        RSA2 rSA = new RSA2();
        public static string PublicKey;
        public static string PrivateKey;

        string filePath = string.Empty;   //文件路径

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            this.openFileDialogOne.Filter = "文本文件|*.txt";
            if (this.openFileDialogOne.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string path = this.openFileDialogOne.FileName;
                    filePath = path;
                    this.textBoxFilePath.Text = path;
                    this.richTextBoxValue.LoadFile(path, RichTextBoxStreamType.PlainText);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.textBoxFilePath.Text.Length > 0)
                {
                    PublicKey = rSA.GetPublicKey();
                    PrivateKey = rSA.GetPrivateKey();
                    string strPath = this.textBoxFilePath.Text;//要加密文件绝对路径
                    string folderPath = strPath.Substring(0, strPath.LastIndexOf("\\") + 1);//根目录
                    string fileName = strPath.Substring(strPath.LastIndexOf("\\") + 1, strPath.LastIndexOf(".") - strPath.LastIndexOf("\\") - 1);//文件名
                    fileName += "(加密).txt";
                    string encryptedFilePath = folderPath + fileName;//加密后得到文件的绝对路径
                    rSA.Encrypt(strPath, encryptedFilePath, PublicKey);
                    this.richTextBoxValue.LoadFile(encryptedFilePath, RichTextBoxStreamType.PlainText);
                    this.textBoxFilePath.Text = encryptedFilePath;
                    MessageBox.Show("文件加密成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            string strPath = this.textBoxFilePath.Text;//要加密文件绝对路径
            string folderPath = strPath.Substring(0, strPath.LastIndexOf("\\") + 1);//根目录
            string fileName = strPath.Substring(strPath.LastIndexOf("\\") + 1, strPath.LastIndexOf(".") - strPath.LastIndexOf("\\") - 1);//文件名
            fileName += "(解密).txt";
            string encryptedFilePath = folderPath + fileName;//加密后得到文件的绝对路径
            rSA.Decrypt(strPath, encryptedFilePath, PrivateKey);
            this.richTextBoxValue.LoadFile(encryptedFilePath, RichTextBoxStreamType.PlainText);
            this.textBoxFilePath.Text = encryptedFilePath;
            MessageBox.Show("文件解密成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
